import { defineStore } from 'pinia'
import type { AdminUser, AdminRole, Department, User } from '@/types/admin'

interface AdminState {
  adminUsers: AdminUser[]
  roles: AdminRole[]
  departments: Department[]
  users: User[]
  currentAdminUser: AdminUser | null
  currentRole: AdminRole | null
  currentDepartment: Department | null
  currentUser: User | null
  total: number
  loading: boolean
}

export const useAdminStore = defineStore('admin', {
  state: (): AdminState => ({
    adminUsers: [],
    roles: [],
    departments: [],
    users: [],
    currentAdminUser: null,
    currentRole: null,
    currentDepartment: null,
    currentUser: null,
    total: 0,
    loading: false
  }),

  getters: {
    getAdminUsers: (state) => state.adminUsers,
    getRoles: (state) => state.roles,
    getDepartments: (state) => state.departments,
    getUsers: (state) => state.users,
    getCurrentAdminUser: (state) => state.currentAdminUser,
    getCurrentRole: (state) => state.currentRole,
    getCurrentDepartment: (state) => state.currentDepartment,
    getCurrentUser: (state) => state.currentUser,
    getTotal: (state) => state.total,
    isLoading: (state) => state.loading,
    getDepartmentTree: (state) => {
      // 构建部门树结构
      const buildTree = (departments: Department[], parentId: number | null = null): Department[] => {
        return departments
          .filter(dept => dept.parent_id === parentId)
          .map(dept => ({
            ...dept,
            children: buildTree(departments, dept.id)
          }))
      }
      return buildTree(state.departments)
    }
  },

  actions: {
    // 管理员用户相关
    async fetchAdminUsers(params?: any) {
      this.setLoading(true)
      try {
        // TODO: 调用获取管理员列表API
        // const response = await getAdminUsersApi(params)
        // this.setAdminUsers(response.data.data)
        // this.setTotal(response.data.total)
        // return response
      } catch (error) {
        throw error
      } finally {
        this.setLoading(false)
      }
    },

    async createAdminUser(userData: Partial<AdminUser>) {
      try {
        // TODO: 调用创建管理员API
        // const response = await createAdminUserApi(userData)
        // await this.fetchAdminUsers()
        // return response
      } catch (error) {
        throw error
      }
    },

    async updateAdminUser(id: number, userData: Partial<AdminUser>) {
      try {
        // TODO: 调用更新管理员API
        // const response = await updateAdminUserApi(id, userData)
        // await this.fetchAdminUsers()
        // return response
      } catch (error) {
        throw error
      }
    },

    async deleteAdminUser(id: number) {
      try {
        // TODO: 调用删除管理员API
        // const response = await deleteAdminUserApi(id)
        // await this.fetchAdminUsers()
        // return response
      } catch (error) {
        throw error
      }
    },

    // 角色相关
    async fetchRoles(params?: any) {
      try {
        // TODO: 调用获取角色列表API
        // const response = await getRolesApi(params)
        // this.setRoles(response.data)
        // return response
      } catch (error) {
        throw error
      }
    },

    async createRole(roleData: Partial<AdminRole>) {
      try {
        // TODO: 调用创建角色API
        // const response = await createRoleApi(roleData)
        // await this.fetchRoles()
        // return response
      } catch (error) {
        throw error
      }
    },

    async updateRole(id: number, roleData: Partial<AdminRole>) {
      try {
        // TODO: 调用更新角色API
        // const response = await updateRoleApi(id, roleData)
        // await this.fetchRoles()
        // return response
      } catch (error) {
        throw error
      }
    },

    async deleteRole(id: number) {
      try {
        // TODO: 调用删除角色API
        // const response = await deleteRoleApi(id)
        // await this.fetchRoles()
        // return response
      } catch (error) {
        throw error
      }
    },

    // 部门相关
    async fetchDepartments() {
      try {
        // TODO: 调用获取部门列表API
        // const response = await getDepartmentsApi()
        // this.setDepartments(response.data)
        // return response
      } catch (error) {
        throw error
      }
    },

    async createDepartment(deptData: Partial<Department>) {
      try {
        // TODO: 调用创建部门API
        // const response = await createDepartmentApi(deptData)
        // await this.fetchDepartments()
        // return response
      } catch (error) {
        throw error
      }
    },

    async updateDepartment(id: number, deptData: Partial<Department>) {
      try {
        // TODO: 调用更新部门API
        // const response = await updateDepartmentApi(id, deptData)
        // await this.fetchDepartments()
        // return response
      } catch (error) {
        throw error
      }
    },

    async deleteDepartment(id: number) {
      try {
        // TODO: 调用删除部门API
        // const response = await deleteDepartmentApi(id)
        // await this.fetchDepartments()
        // return response
      } catch (error) {
        throw error
      }
    },

    // 用户相关
    async fetchUsers(params?: any) {
      this.setLoading(true)
      try {
        // TODO: 调用获取用户列表API
        // const response = await getUsersApi(params)
        // this.setUsers(response.data.data)
        // this.setTotal(response.data.total)
        // return response
      } catch (error) {
        throw error
      } finally {
        this.setLoading(false)
      }
    },

    async createUser(userData: Partial<User>) {
      try {
        // TODO: 调用创建用户API
        // const response = await createUserApi(userData)
        // await this.fetchUsers()
        // return response
      } catch (error) {
        throw error
      }
    },

    async updateUser(id: number, userData: Partial<User>) {
      try {
        // TODO: 调用更新用户API
        // const response = await updateUserApi(id, userData)
        // await this.fetchUsers()
        // return response
      } catch (error) {
        throw error
      }
    },

    async deleteUser(id: number) {
      try {
        // TODO: 调用删除用户API
        // const response = await deleteUserApi(id)
        // await this.fetchUsers()
        // return response
      } catch (error) {
        throw error
      }
    },

    // 设置状态方法
    setAdminUsers(adminUsers: AdminUser[]) {
      this.adminUsers = adminUsers
    },

    setRoles(roles: AdminRole[]) {
      this.roles = roles
    },

    setDepartments(departments: Department[]) {
      this.departments = departments
    },

    setUsers(users: User[]) {
      this.users = users
    },

    setCurrentAdminUser(adminUser: AdminUser) {
      this.currentAdminUser = adminUser
    },

    setCurrentRole(role: AdminRole) {
      this.currentRole = role
    },

    setCurrentDepartment(department: Department) {
      this.currentDepartment = department
    },

    setCurrentUser(user: User) {
      this.currentUser = user
    },

    setTotal(total: number) {
      this.total = total
    },

    setLoading(loading: boolean) {
      this.loading = loading
    },

    // 重置状态
    resetState() {
      this.adminUsers = []
      this.roles = []
      this.departments = []
      this.users = []
      this.currentAdminUser = null
      this.currentRole = null
      this.currentDepartment = null
      this.currentUser = null
      this.total = 0
      this.loading = false
    }
  }
})